package com.hsnr.familyfinder;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class DrawView extends View {
    private final String TAG = "FamilyFinder";

    Paint[] p;
    Paint start_out;
    Paint start;
    HashMap<String, String> startpunkt = null;
    ArrayList<HashMap<String, String>> coordinateData;

    /**
     * @param context
     * @param attrs
     */
    public DrawView(Context context, AttributeSet attrs) {
        super(context, attrs);
        start = new Paint();
        start_out = new Paint();

        p = new Paint[4];
        p[0] = new Paint();
        p[1] = new Paint();
        p[2] = new Paint();
        p[3] = new Paint();

        start.setColor(Color.BLACK);
        start_out.setColor(Color.CYAN);
        p[0].setColor(Color.RED);
        p[1].setColor(Color.YELLOW);
        p[2].setColor(Color.GREEN);
        p[3].setColor(Color.BLUE);

    }

    /**
     * @return the coordinateData
     */
    public ArrayList<HashMap<String, String>> getCoordinateData() {
        return coordinateData;
    }

    /**
     * @param coordinateData
     *            the coordinateData to set
     */
    public void setCoordinateData(ArrayList<HashMap<String, String>> coordinateData) {
        this.coordinateData = coordinateData;
    }

    @Override
    public void onDraw(Canvas canvas) {
        HashMap<String, Double> data = computeLageplan();
        if (data != null) {
            for (int i = 0; i < 4; i++) {
                if (data.containsKey("x" + i) && data.containsKey("y" + i)) {
                    canvas.drawCircle(data.get("x" + i).floatValue(), data.get("y" + i).floatValue(), 5F, p[i]);
                }
            }
            if (startpunkt != null) {
                float x, y;
                if (Double.parseDouble(startpunkt.get("lon")) > data.get("laengeMax")) {
                    x = this.getHeight() - 15F;
                } else if (Double.parseDouble(startpunkt.get("lon")) < data.get("laengeMin")) {
                    x = 15F;
                } else {
                    x = new Double(startpunkt.get("x")).floatValue();
                    if (x > this.getHeight())
                        x = this.getHeight() - 15F;
                }

                if (Double.parseDouble(startpunkt.get("lat")) > data.get("breiteMax")) {
                    y = this.getWidth() - 15F;
                } else if (Double.parseDouble(startpunkt.get("lat")) < data.get("breiteMin")) {
                    y = 15F;
                } else {
                    y = new Double(startpunkt.get("y")).floatValue();
                    if (y > this.getWidth())
                        y = this.getWidth() - 15F;
                }

                canvas.drawCircle(x, y, 5F, start_out);
            }
            canvas.drawText(String.valueOf(Math.round(data.get("laengeMax") * 10000d) / 10000d), this.getWidth() - 50F, 10F,
                    start);
            canvas.drawText(String.valueOf(Math.round(data.get("laengeMin") * 10000d) / 10000d), 0F, 10F, start);
            canvas.drawText(String.valueOf(Math.round(data.get("breiteMin") * 10000d) / 10000d), 0F, 30F, start);
            canvas.drawText(String.valueOf(Math.round(data.get("breiteMax") * 10000d) / 10000d), 0F, this.getHeight() - 10F,
                    start);
        }
    }

    /**
     * 
     * @param lng
     *            array with longitude values
     * @param lat
     *            array with latitude values
     * @param isStart
     *            is starting point
     * @return String array x/y
     */
    public HashMap<String, Double> computeLageplan() {
        ArrayList<HashMap<String, String>> list = getCoordinateData();
        double breiteMin = 90;
        double breiteMax = -90;
        double laengeMin = 180;
        double laengeMax = -180;
        double diffBreite = Double.MIN_VALUE;
        double diffLaenge = Double.MIN_VALUE;
        HashMap<String, Double> ret = new HashMap<String, Double>();

        // Max und min ermitteln
        boolean istStartpunkt;
        double lat, lon;
        double x, y;
        for (HashMap<String, String> h : list) {
            lat = Double.valueOf(h.get("lat"));
            lon = Double.valueOf(h.get("lon"));
            istStartpunkt = h.get("isStart").equals("1");
            if (istStartpunkt == true) {
                startpunkt = h;
                continue;
            }
            if (lat > breiteMax) {
                breiteMax = lat;
            }
            if (lon > laengeMax) {
                laengeMax = lon;
            }
            if (lat < breiteMin) {
                breiteMin = lat;
            }
            if (lon < laengeMin) {
                laengeMin = lon;
            }

        }
        diffBreite = breiteMax - breiteMin;
        diffLaenge = laengeMax - laengeMin;

        double diffMax = 0;
        if (diffBreite > diffLaenge) {
            diffMax = diffBreite;
        } else {
            diffMax = diffLaenge;
        }
        // Punkte einzeichnen
        double yMinRange = 0.1 * this.getHeight();
        double xMinRange = 0.1 * this.getWidth();
        double yMaxRange = 0.9 * this.getHeight();
        double xMaxRange = 0.9 * this.getWidth();
        double diffXRange = xMaxRange - xMinRange;
        double diffYRange = yMaxRange - yMinRange;
        double diffMaxRange = 0;
        if (diffXRange > diffYRange) {
            diffMaxRange = diffXRange;
        } else {
            diffMaxRange = diffYRange;
        }
        int i = 0;

        ret.put("laengeMax", laengeMax);
        ret.put("breiteMax", breiteMax);
        ret.put("laengeMin", laengeMin);
        ret.put("breiteMin", breiteMin);

        for (HashMap<String, String> h : list) {
            lat = Double.valueOf(h.get("lat"));
            lon = Double.valueOf(h.get("lon"));
            istStartpunkt = h.get("isStart").equals("1");
            if (istStartpunkt)
                continue;
            x = xMinRange + (lon - laengeMin) / diffMax * diffMaxRange;
            y = yMinRange + (lat - breiteMin) / diffMax * diffMaxRange;
            ret.put("x" + String.valueOf(i), x);
            ret.put("y" + String.valueOf(i), y);
            i++;
        }
        if (startpunkt != null) {
            lat = Double.valueOf(startpunkt.get("lat"));
            lon = Double.valueOf(startpunkt.get("lon"));
            startpunkt.put("x", String.valueOf(xMinRange + (lon - laengeMin) / diffMax * diffMaxRange));
            startpunkt.put("y", String.valueOf(yMinRange + (lat - breiteMin) / diffMax * diffMaxRange));
            i++;
        }
        return ret;

    }
}